package com.manager.entity.factory;

import cn.hutool.extra.spring.SpringUtil;
import com.manager.entity.dao.Manager;
import com.manager.entity.dto.CampusDto;
import com.manager.entity.dto.CampusManagerDto;
import com.manager.entity.dto.ManagerDto;
import com.manager.entity.vo.ManagerVo;
import com.manager.service.ICampusService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

public class ManagerFactory {
    public static final ManagerConverter MANAGER_CONVERTER = Mappers.getMapper(ManagerConverter.class);

    @Mapper
    public interface ManagerConverter {
        default Manager toManager(ManagerVo managerVo){
            if(managerVo==null){
                return null;
            }
            Manager manager = new Manager();
            manager.setPwd(managerVo.getPwd());
            manager.setAccount(managerVo.getAccount());
            manager.setCId(managerVo.getCId());
            manager.setEnable(managerVo.getEnable());
            manager.setInfo(managerVo.getInfo());
            return manager;
        };

        default ManagerDto toManagerDto(Manager manager){
            if (manager == null) {
                return null;
            } else {
                ManagerDto.ManagerDtoBuilder managerDto = ManagerDto.builder();
                managerDto.id(manager.getId());
                managerDto.account(manager.getAccount());
                managerDto.info(manager.getInfo());
                managerDto.enable(manager.getEnable());
                managerDto.createTime(manager.getCreateTime());
                managerDto.updateTime(manager.getUpdateTime());
                managerDto.identity(manager.getIdentity());
                managerDto.campusDto(CIdToCampusDto.toCampusDto(manager.getCId()));
                return managerDto.build();
            }
        }

        default CampusManagerDto toCampusManagerDto(Manager manager){
            if (manager == null) {
                return null;
            } else {
                CampusManagerDto.CampusManagerDtoBuilder managerDto = CampusManagerDto.builder();
                managerDto.id(manager.getId());
                managerDto.account(manager.getAccount());
                managerDto.info(manager.getInfo());
                managerDto.enable(manager.getEnable());
                managerDto.createTime(manager.getCreateTime());
                managerDto.updateTime(manager.getUpdateTime());
                managerDto.identity(manager.getIdentity());
                return managerDto.build();
            }
        }

        List<ManagerDto> toListManagerDto(List<Manager> manager);

        List<CampusManagerDto> toListCampusManagerDto(List<Manager> managers);
    }

    public interface CIdToCampusDto{
        /**
         * 根据校区ID从数据库查询,获取具体的校区实体类数据
         */
        static CampusDto toCampusDto(Integer cid){
            if(cid==null){
                return null;
            }
            ICampusService iCampusService = SpringUtil.getBean(ICampusService.class);
            return iCampusService.getCampusById(cid);
        }
    }
}
